﻿using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// 百度UEditor编辑器asp.net上传组件
/// buzhizhe 
/// 2011.10.21
/// </summary>
namespace Web.myadmin
{

    public partial class upload : System.Web.UI.Page
    {
        public string url;
        private string _uploadPath = "../uploadfiles/"; //上传文件保存路径
        private int _FileSizeMax = 1000; //上传文件最大限制（单位kb）
        private string state = "SUCCESS";
        string[] filetypes = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };

        /// <summary>
        /// 判定系统是否允许上传，建议用session判定
        /// </summary>
        /// <returns>返回true则允许上传，false则不允许上传</returns>
        private bool AllowUpload()
        {
            //请在此处写入判断代码
            //if (this.Session["loginUserName"] == null) return false;

            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            url = Request.QueryString["url"];
            string strPath = Request.QueryString["path"];
            if (strPath != null && strPath != "") _uploadPath = strPath; //从外部获取图片保存路径

            if (Request.Files.Count > 0)
            {
                if (!AllowUpload())
                {
                    Response.Write("<script type='text/javascript'>alert('对不起，系统没有开启上传权限。');location.href='upload.aspx'</script>");
                    Response.End();
                }
                uploadfile();

            }
        }

        /// <summary>
        /// 上传文件到服务器
        /// </summary>
        private void uploadfile()
        {
            HttpPostedFile file = Request.Files[0];
            string title = Request.Form["pictitle"];
            if (!isValidateFileType(Path.GetExtension(file.FileName)))
            {
                state = "TYPE";
            }
            if (file.ContentLength > _FileSizeMax * 1024)
            {
                state = "SIZE";
            }
            string oldFileName = Path.GetFileName(file.FileName);
            string fileExt = Path.GetExtension(file.FileName);
            string fileName =  DateTime.Now.ToString("yyMMdd_hhmmss") + (new Random()).Next(1, 99)+ "_" +oldFileName.Replace(fileExt, "") ;
            string fullFileName = _uploadPath + fileName + fileExt;
            string savePath = Server.MapPath(".") + "/" + fullFileName;
            try
            {
                file.SaveAs(savePath);
            }
            catch
            {
                state = "ERROR";
            }

            Response.Write("{'url':'" + fullFileName + "','title':'" + title + "','state':'" + state + "'}");
        }



        /// <summary>
        /// 判断是否合法的扩展名
        /// </summary>
        /// <param name="ext"></param>
        /// <returns></returns>
        private bool isValidateFileType(string ext)
        {
            ext = ext.ToLower();
            bool ret = false;
            foreach (string i in filetypes)
            {
                if (i == ext || i == "." + ext) ret = true;
            }
            return ret;
        }


    }
}